/*
 * FileName: WorkStatusCollector.java
 *
 * Company: 北京中科金财科技股份有限公司
 * 
 */
package com.sinodata.bsm.cicp.collector.informix;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: informix数据库运行状态采集器
 * 
 * 在安装informix数据库的主机上执行：onstat - 可以显示数据库状态信息
 * 
 * 正常情况下是：
 *  IBM Informix Dynamic Server Version 9.40.FC7     -- On-Line -- Up 35 days 16:51:16 -- 3920896 Kbytes
 * 长事务情况下是：
 *  IBM Informix Dynamic Server Version 9.40.FC7     -- On-Line (LONGTX) -- Up 35 days 16:41:40 -- 3920896 Kbytes
 * 有检查点的情况下是：
 *  IBM Informix Dynamic Server Version 9.40.FC7     -- On-Line (CHKP) -- Up 35 days 16:41:40 -- 3920896 Kbytes
 *                  
 * </p>
 *
 * @author wangpeng
 * @version 3.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-18 下午4:10:12          wangpeng        3.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class WorkStatusCollector extends Collector {
    private static Logger logger = Logger.getLogger(WorkStatusCollector.class);
    //private static String cmd = "onstat -";
    private static String regex = ".*--\\s+On-Line\\s*(.*)\\s*--\\s+Up.*";

    @Override
    public String[] getPropMeta() {
        return new String[] { "WorkStatus" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "cmd" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String[] propMetas = this.getPropMeta();
        String cmd = this.getParamMap().get("cmd");
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue resPropertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, cmd, 0, 0);
            String strLine = null;
            Pattern pattern = Pattern.compile(regex);
            while ((strLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    String status = matcher.group(1).trim();
                    if (status.length() == 8) {
                        propValues[0] = ResPropertyValue.createPropValue("长事务", propMetas[0]);
                    } else if (status.length() == 6) {
                        propValues[0] = ResPropertyValue.createPropValue("有检查点", propMetas[0]);
                    } else {
                        propValues[0] = ResPropertyValue.createPropValue("正常", propMetas[0]);
                    }
                    resPropertyValueX.setValues(propValues);
                    values.add(resPropertyValueX);
                }
            }
        } catch (IOException e) {
            logger.error("failed to collect informix working status", e);
            return null;
        }
        return values;
    }

}
